//待付款、待发货、待收货、售后
function down(){
    var m,h;
    var open=true;
    for(var i=1;i<5;i++){
        var n=m+i;
        n=document.getElementById("a"+i);
        n.onclick=function () {
            for(var i=1;i<5;i++){
                var n=m+i;
                n=document.getElementById("a"+i);
                n.style.background="ghostwhite";
                n.style.textDecoration="none";
            }
            this.style.background="white";
            for(var j=1;j<5;j++){
                var k=h+j;
                k=document.getElementById("b"+j);
                if(j==this.id.substr(1)){
                    k.style.display="block";
                }else{
                    k.style.display="none";
                }
            }

        }
    }
}
down();
// 商品计件
var count=0;
$(".reduce").on("click",function () {
    if(parseInt($(this).siblings("input").val())>1){
        count=parseInt($(this).siblings("input").val())-1;
        $(this).siblings(".shop_count").val(count);
    }else{
        $(this).siblings(".shop_count").val("1");
    }
});
$(".plus").on("click",function () {
        count=parseInt($(this).siblings("input").val())+1;
        $(this).siblings(".shop_count").val(count);
});
$(".shop_count").on("change",function () {
    console.log($(this).val());
    if(!$(this).val()){//为空时
        $(this).val("1");
    }
});
//复选框选择
function xuand(obj) {
    if (obj.checked==true) {
        obj.style.background = "url(/Public/Moble_home/images/xuan.png)";
        obj.style.backgroundSize="14px";
        obj.style.outline="none";
    } else {
        obj.style.background = "url(/Public/Moble_home/images/wei.png)";
        obj.style.backgroundSize="14px";
        open2=false;
        $("#all_check").css({
            "background-image":"url(/Public/Moble_home/images/wei.png)",
            "background-size":"14px"
        });
    }
}
//全选
var open2=true;
$("#all_check").click(function () {
    if(open2==true){
        $(".input_check").css({
            "background-image":"url(/Public/Moble_home/images/xuan.png)",
            "background-size":"14px",
            "outline":"none"
        });
        open2=false;
    }else{
        $(".input_check").css({
            "background-image":"url(/Public/Moble_home/images/wei.png)",
            "background-size":"14px"
        });
        open2=true;
    }
});


  //创建一个购物车对象
function Cart(){
      //先判断是否已经是订单
      this.is_order=function(){
          data_length = $(".input_check").length;
          for(var i=0;i<data_length;i++){
              if($(".input_check")[i].checked==true){
              /*$(".xuan")[i].nextSibling.className = "ihover";*/
                 $(".input_check").css({
                     "background-image":"url(/Public/Moble_home/images/xuan.png)",
                     "background-size":"14px",
                     "outline":"none"
                });
              }
          }
      }

      //订单总价钱 
      this.get_total_price=function(){
          var url = '/index.php/Home/Cart/get_total_price';
          $.ajax({
              type : "GET",
              url  : url,
              error : function(requeste){
                  alert("服务器繁忙, 请联系管理员!");
                  return;
              },
              success : function(v){
                  $(".total_price").html("¥"+parseInt(v));
              }
          });
      }

      //当商品数量发生变化时，发送数据
      my_this = this;
      this.when_chang_num=function(){
          $('.reduce').on('click',function(){
          //将购物车表id和数量给发送后台
          var id = $(this).parent().parent().parent().siblings(".xuandiv").children(".input_check").attr("title");
          var value = $(this).siblings(".shop_count").val();
          var url = '/index.php/Home/Cart/get_num/cart_id/'+id+"/goods_num/"+value;
          var _this = $(this);
          $.ajax({
              type : "GET",
              url  : url,
              error: function(request) {
                  alert("服务器繁忙, 请联系管理员!");
              return;
              },
              success: function(v) {
                  _this.parent().parent().parent().siblings(".xuandiv").children(".input_check").attr("total_price",parseInt(v));
                  my_this.get_total_price();
              }
              });
          });

          $('.plus').on('click',function(){
          //将购物车表id和数量给发送后台
          var id = $(this).parent().parent().parent().siblings(".xuandiv").children(".input_check").attr("title");
          var value = $(this).siblings(".shop_count").val();
          var url = '/index.php/Home/Cart/get_num/cart_id/'+id+"/goods_num/"+value;
          var _this = $(this);
          $.ajax({
              type : "GET",
              url  : url,
              error: function(request) {
                  alert("服务器繁忙, 请联系管理员!");
              return;
              },
              success: function(v) {
                  _this.parent().parent().parent().siblings(".xuandiv").children(".input_check").attr("total_price",parseInt(v));
                  my_this.get_total_price();
              }
              });
          });


      }

      //当点击选择按钮时
      this.when_click_input=function(){
          $('.xuan').on('click',function(){
      
              //判断商品是否被选中
              //如果商品被选中，则is_order为1。否则is_order为2
      
              if(this.checked == false){
                  //如果没有被选中，则获取当前的name值
                  var this_title = this.title;
                  var url = '/index.php/Home/Cart/cancel_order/cart_id/'+this_title;
                  $.ajax({
                      type : "GET",
                      url  : url,
                      error: function(request) {
                          alert("服务器繁忙, 请联系管理员!");
                          return;
                      },
                      success: function(v) {
                          my_this.get_total_price();
                      }
                  });
              }else{
                  var url = '/index.php/Home/Cart/enter';
                  $.ajax({
                      type : "POST",
                      url  : url,
                      data : $("#cart_post").serialize(),
                      error: function(request) {
                          alert("服务器繁忙, 请联系管理员!");
                          return;
                      },
                      success: function(v) {
                          my_this.get_total_price();
                      }
                  });
              }
          });

          //点击全选时
          $('.quanxuan').on('click',function(){
              if(this.checked == true){
              var url = '/index.php/Home/Cart/checked_all';
              $.ajax({
                  type : "GET",
                  url  : url,
                  error: function(request) {
                      alert("服务器繁忙, 请联系管理员!");
                      return;
                  },
                  success: function(v) {
                      my_this.get_total_price();
                  }
              });
             }else{
               var url = '/index.php/Home/Cart/no_checked_all';
               $.ajax({
                  type : "GET",
                  url  : url,
                  error: function(request) {
                      alert("服务器繁忙, 请联系管理员!");
                      return;
                  },
                  success: function(v) {
                      my_this.get_total_price();
                  }
               });
           }
        });
    }

     //主动改变数值时
    this.change_num = function(){
         var shopNumber = $(".shop_count");
         $.each(shopNumber,function(){
            $(this).change(function(){
                 var id = $(this).parent().parent().parent().siblings(".xuandiv").children(".input_check").attr("title");
                 var value = $(this).val();
                 var url = '/index.php/Home/Cart/get_num/cart_id/'+id+"/goods_num/"+value;
                 var _this = $(this);
                 $.ajax({
                     type : "GET",
                     url  : url,
                     error: function(request) {
                         alert("服务器繁忙, 请联系管理员!");
                     return;
                    },
                    success: function(v) {
                        _this.parent().parent().parent().siblings(".xuandiv").children(".input_check").attr("total_price",parseInt(v));
                        my_this.get_total_price();
                    }
                 });
             });
             my_this.get_total_price();
        });
    }   

    //删除购物车商品时
  this.delete_goods = function(){
    var del = $('.del');
      $.each(del,function (){
        $(this).click(function(){
          var _this = this;
          var __this = $(this);
          layer.confirm('是否确定删除此商品?', {icon: 3, title:'删除'},function(index){
            //删除节点
            __this.parent().parent().parent().remove();
            var url = _this.title;
            $.ajax({
              type:"get",
              url : url
            });
            my_this.get_total_price();
            layer.close(index);
          })
          return false;
        });
      });
  }
}
    var myCart = new Cart();
    myCart.is_order();
    myCart.get_total_price();
    myCart.when_chang_num();
    myCart.when_click_input();
    myCart.change_num();
    myCart.delete_goods();